/*
 * Copyright (c) 2021 Eshel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.android.basic

import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Camera
import android.graphics.Color
import android.util.TypedValue
import android.view.View
import androidx.annotation.ColorInt

/**
 * <br>createBy Gw
 * <br>createTime: 2021/6/24 10:41
 */
inline val Float.px: Float get() = this
inline val Double.px: Float get() = this.toFloat()
inline val Int.px: Float get() = this.toFloat()

val Float.dp: Float get() = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP, this, Resources.getSystem().displayMetrics)
inline val Double.dp: Float get() = float.dp
inline val Int.dp: Float get() = float.dp

val Float.sp: Float get() = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_SP, this, Resources.getSystem().displayMetrics)
inline val Double.sp: Float get() = float.sp
inline val Int.sp: Float get() = float.sp

inline val String.color get() = Color.parseColor(this)

inline val Int.float get() = toFloat()
inline val Double.float get() = toFloat()
inline val Long.float get() = toFloat()

inline val Float.int get() = toInt()
inline val Double.int get() = toInt()
inline val Long.int get() = toInt()

inline val Int.long get() = toLong()
inline val Float.long get() = toLong()
inline val Double.long get() = toLong()

inline val Int.double get() = toDouble()
inline val Float.double get() = toDouble()
inline val Long.double get() = toDouble()

inline val View.centerX get() = width.toFloat() / 2
inline val View.centerY get() = height.toFloat() / 2

inline val Bitmap.centerX get() = width.toFloat() / 2
inline val Bitmap.centerY get() = height.toFloat() / 2

inline val View.realLeft get() = paddingLeft.toFloat()
inline val View.realTop get() = paddingTop.toFloat()
val View.realRight get() = (right - left - paddingRight).toFloat()
val View.realBottom get() = (bottom - top - paddingBottom).toFloat()

fun dp2px(value: Float): Float {
    return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, value, Resources.getSystem().displayMetrics
    )
}

fun sp2px(value: Int): Int {
    return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_SP, value.toFloat(), Resources.getSystem().displayMetrics
    ).toInt()
}

fun application(): BasicApplication {
    return BasicApplication.application
}

fun resources(): Resources {
    return Resources.getSystem()
}

fun View.decodeBitmap(id: Int, targetWidth: Int): Bitmap {
    return decodeResource(resources, id, targetWidth)
}

fun decodeResource(resources: Resources, id: Int, targetWidth: Int): Bitmap{
    val options = BitmapFactory.Options()
    options.inJustDecodeBounds = true
    BitmapFactory.decodeResource(resources, id, options)
    options.inDensity = options.outWidth
    options.inTargetDensity = targetWidth
    options.inJustDecodeBounds = false
    return BitmapFactory.decodeResource(resources, id, options)
}

inline fun Camera.withSave(block: Camera.() -> Unit){
    save()
    block.invoke(this)
    restore()
}
